package com.gton.person.commons;

import com.gton.person.commons.excep.DIYException;
import com.gton.person.commons.myenum.ExcptionEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @program: PersonalDesign
 * @description: 统一返回格式
 * @author: GuoTong
 * @create: 2020-11-12 17:09
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class ResultAdvice {
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 返回的提示信息
     */
    private String msg;
    /**
     * 返回的数据
     */
    private Object data;

    /**
     * 无消息成功
     */
    public static ResultAdvice OK() {
        return new ResultAdvice(200, "成功", null);
    }

    /**
     * 带回调消息成功
     */
    public static ResultAdvice OK(Object data) {
        return new ResultAdvice(200, "成功", data);
    }

    /**
     * 无消息成功
     */
    public static ResultAdvice error(String msg, Object data) {
        return new ResultAdvice(500, msg, data);
    }

    /**
     * 带回调消息成功
     */
    public static ResultAdvice error(String msg) {
        return new ResultAdvice(500, msg, null);
    }

    //自定义异常返回的结果
    public static ResultAdvice diyExcption(DIYException de) {
        ResultAdvice result = new ResultAdvice();
        result.setCode(de.getErrorCode());
        result.setMsg(de.getErrorMsg());
        result.setData(null);
        return result;
    }

    //其他异常处理方法返回的结果
    public static ResultAdvice otherError(ExcptionEnum errorEnum) {
        ResultAdvice result = new ResultAdvice();
        result.setMsg(errorEnum.getErrorMsg());
        result.setCode(errorEnum.getErrorCode());
        result.setData(null);
        return result;
    }

}
